#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <fcntl.h>    
#include <sys/mman.h>          // Flags for open()
#include <sys/stat.h>           // Open() system call
#include <sys/types.h>          // Types for open()
#include <unistd.h>             // Close() system call
#include <string.h>             // Memory setting and copying
#include <getopt.h>             // Option parsing
#include <errno.h>              // Error codes
#include <string.h>
#include <poll.h>
#include "util.h"               // Miscellaneous utilities
#include "conversion.h"         // Convert bytes to MiBs
#include "libaxidma.h"          // Interface ot the AXI DMA library
#include <pthread.h>
#include "../udp/testnet.h"
#include "../can/can.h"
#include "../pcm/pcm.h"
#include "demo.h"


extern int change_422;//高速上行通道切换变量
extern uint8_t Recv_pcm0[];
extern uint8_t Recv_pcm1[];
extern unsigned char tx2_sar[];//SAR载荷处理数据回传
int dma422_end=0, count_pcm=0, count_gtxsd=0;//422接收到AI星历等数据、422接收到数传重构数据、GTX发送数据数量、AI数图像数据发送计数
int g_titcks = 100;
pthread_mutex_t g_lock;

axidma_dev_t axidma_dev;
struct dma_transfer trans;
struct dma_transfer trans_1;
struct dma_transfer trans_gtx;

/* 测试 */
int test_pcm, test_pcm_1, rs422_flg, text_sar;
unsigned char test_read[996];//SAR载荷原始数据缓冲区2+992+2
unsigned char rbuffer[RMAXLENGTH] = { 0 };  //422收数据1020
unsigned char ru_buffer[SMAXLENGTH] = { 0 };  //422数据UDP转发992字节
unsigned char rg_buffer[SMAXLENGTH+4] = { 0 }; //422数据GTX传输992+4字节
unsigned char sbuffer[MAXLENGTH] = { 0 };
unsigned char test_gtx[992] = {0x36,0xDB,0xC6,0xA4,0x3E,0x90,0xAE,0x80,0x2B,0x5B,0x54,0x87,0x95,0xC5,0x60,0x67,0xD3,0xC4,0x9C,0x71,0x4F,0x63,0xA6,0x28,0x00,0xAF,0x85,0xCB,0x97,0xF5,0x37,0x8C,0x08,0xF8,0xC4,0x85,0x4E,0xB8,0xB0,0x75,0xED,0x34,0x57,0x65,0x7A,0x0B,0x49,0xCB,0x89,0x4F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0x0C,0x74,0x01,0x43,0x9B,0x19,0x74,0xAE,0x86,0xCC,0xDD,0x8F,0xAB,0xA3,0x2E,0x2A,0xAF,0x74,0xE9,0x47,0x1C,0xD7,0x54,0xEB,0xDE,0xE6,0xB8,0xE0,0xB6,0x5F,0xEE,0x4A,0xD8,0xF9,0x6E,0xF9,0x10,0x9C,0x8C,0x6C,0x3B,0x3D,0xE8,0x28,0xF9,0x21,0x9F,0xFB,0x79,0xCF,0x60,0x66,0x84,0xB1,0x4D};
const char *file_pcm1 = "./pcm_A.dat";
const char *file_pcm2 = "./pcm_B.txt";
const char *file_sar1 = "CH1.dat";
const char *file_sar2 = "CH2.dat";
extern int flag_textsar;

/* 数据写入txt文件 */
int data422_write(const char *filename, unsigned char *data, int len)
{
    FILE* fptr = fopen(filename , "a");
	if (fptr == NULL)
	{
	    puts("Fail to open file!");
	    return -1;
	}
    for(int i=0; i<len; i++)
    {
        if(((i%30) == 0)&&(i>0))
            fprintf(fptr, "\n");
        fprintf(fptr, "%02x\t", data[i]);
    }
    fprintf(fptr, "\n");
    fprintf(fptr, "\n");
    fclose(fptr);
    return 0;
}

/* Prints the usage for this program */
static void print_usage(bool help) {
	FILE* stream = (help) ? stdout : stderr;
	fprintf(stream, "Usage: axidma_transfer  "
			"[-t <DMA tx channel>] [-r <DMA rx channel>] [-s <Output file size>"
			" | -o <Output file size>].\n");
	if (!help) {
		return;
	}
	// fprintf(stream, "\t<input path>:\t\tThe path to file to send out over AXI "
	//         "DMA to the PL fabric. Can be a relative or absolute path.\n");
	// fprintf(stream, "\t<output path>:\t\tThe path to place the received data "
	//         "from the PL fabric into. Can be a relative or absolute path.\n");
	fprintf(stream, "\t-t <DMA tx channel>:\tThe device id of the DMA channel "
			"to use for transmitting the file. Default is to use the lowest "
			"numbered channel available.\n");
	fprintf(stream, "\t-r <DMA rx channel>:\tThe device id of the DMA channel "
			"to use for receiving the data from the PL fabric. Default is to "
			"use the lowest numbered channel available.\n");
	fprintf(stream, "\t-s <Output file size>:\tThe size of the output file in "
			"bytes. This is an integer value that must be at least the number "
			"of bytes received back. By default, this is the same as the size "
			"of the input file.\n");
	fprintf(stream, "\t-o <Output file size>:\tThe size of the output file in "
			"Mibs. This is a floating-point value that must be at least the "
			"number of bytes received back. By default, this is the same "
			"the size of the input file.\n");
	return;
}

/* Parses the command line arguments overriding the default transfer sizes,
 * and number of transfer to use for the benchmark if specified. */
static int parse_args(int argc, char **argv, int *input_channel,
		int *output_channel, int *output_size) {
	char option;
	int int_arg;
	double double_arg;
	bool o_specified, s_specified;
	int rc;

	/* Set the default values for the arguments*/
	*input_channel = -1;
	*output_channel = -1;
	*output_size = -1;
	o_specified = false;
	s_specified = false;
	rc = 0;

	while ((option = getopt(argc, argv, "t:r:s:o:h")) != (char) -1) {
		switch (option) {
		// Parse the transmit channel device id
		case 't':
			rc = parse_int(option, optarg, &int_arg);
			if (rc < 0) {
				print_usage(false);
				return rc;
			}
			*input_channel = int_arg;
			break;

			// Parse the receive channel device id
		case 'r':
			rc = parse_int(option, optarg, &int_arg);
			if (rc < 0) {
				print_usage(false);
				return rc;
			}
			*output_channel = int_arg;
			break;

			// Parse the output file size (in bytes)
		case 's':
			rc = parse_int(option, optarg, &int_arg);
			if (rc < 0) {
				print_usage(false);
				return rc;
			}
			*output_size = int_arg;
			s_specified = true;
			break;

			// Parse the output file size (in MiBs)
		case 'o':
			rc = parse_double(option, optarg, &double_arg);
			if (rc < 0) {
				print_usage(false);
				return rc;
			}
//                *output_size = MIB_TO_BYTE(double_arg);
			o_specified = true;
			break;

		case 'h':
			print_usage(true);
			exit(0);

		default:
			print_usage(false);
			return -EINVAL;
		}
	}

	// If one of -t or -r is specified, then both must be
	if ((*input_channel == -1) ^ (*output_channel == -1)) {
		fprintf(stderr, "Error: Either both -t and -r must be specified, or "
				"neither.\n");
		print_usage(false);
		return -EINVAL;
	}

	// Only one of -s and -o can be specified
	if (s_specified && o_specified) {
		fprintf(stderr, "Error: Only one of -s and -o can be specified.\n");
		print_usage(false);
		return -EINVAL;
	}

	// // Check that there are enough command line arguments
	// if (optind > argc-2) {
	//     fprintf(stderr, "Error: Too few command line arguments.\n");
	//     print_usage(false);
	//     return -EINVAL;
	// }

	// Check if there are too many command line arguments remaining
	if (optind < argc - 2) {
		fprintf(stderr, "Error: Too many command line arguments.\n");
		print_usage(false);
		return -EINVAL;
	}

	// Parse out the input and output paths
	// *input_path = argv[optind];
	// *output_path = argv[optind+1];
	return 0;
}

/* RS422---A接收数据后校验累加和 */
int sum422_check()
{
	int sum = 0;

	for (int i = 26; i < 1016; i++)
	{
		sum += Recv_pcm0[i];
	}

	sum &= 0xffff;

	return sum;
}

/* RS422--B接收数据后校验累加和 */
int sum422_check_1()
{
	int sum = 0;

	for (int i = 26; i < 1016; i++)
	{
		sum += Recv_pcm1[i];
	}

	sum &= 0xffff;

	return sum;
}

/* 测试：读取dat文件数据*/
int data_read(const char *filename1, const char *filename2, int len)
{
	int num;
	int count_ch1 = 0, count_ch2 = 0;
    FILE* fptr1 = fopen(filename1 , "rb");
    FILE* fptr2 = fopen(filename2 , "rb");

	if (fptr1 == NULL)
	{
	    puts("Fail to open read_file1 !");
	    return -1;
	}
	if (fptr2 == NULL)
	{
	    puts("Fail to open read_file2 !");
	    return -1;
	}

	test_read[0] = 0x5C;
	test_read[1] = 0xFB;
	test_read[2] = 0x1A;
	test_read[3] = 0xCF;
	test_read[4] = 0xFC;
	test_read[5] = 0x1D;
	test_read[994] = 0xFD;
	test_read[995] = 0xFE;
	for(int i=858; i<994; i++)
	{
		test_read[i] = 0xFF;
	}


	while( (feof(fptr1) == 0) || (feof(fptr2) == 0) )
	{

	    if( feof(fptr1) == 0 )
	    {
	    	test_read[6] = 0x03;//载荷信道标识
	    	test_read[7] = count_ch1 >> 16;
	    	test_read[8] = count_ch1 >> 8;
	    	test_read[9] = count_ch1;

	    	num = fread(&test_read[10], sizeof(unsigned char), len, fptr1);
	    	if( num != len )
	    	{
	    		printf("Test CH1 ENDING NUM : %d\n", num);
	    		memset(&test_read[10+num], 0xFF, len-num);
	    	}
	    	if( axidma0send(axidma_dev, &trans_gtx, test_read) >= 0 )
	    		count_ch1++;
	    }
	    if( feof(fptr2) == 0 )
	    {
	    	test_read[6] = 0x06;//载荷信道标识
	    	test_read[7] = count_ch2 >> 16;
	    	test_read[8] = count_ch2 >> 8;
	    	test_read[9] = count_ch2;

	    	num = fread(&test_read[10], sizeof(unsigned char), len, fptr2);
	    	if( num != len )
	    	{
	    		printf("Test CH2 ENDING NUM : %d\n", num);
	    		memset(&test_read[10+num], 0xFF, len-num);
	    	}
	    	if( axidma0send(axidma_dev, &trans_gtx, test_read) >= 0 )
	    		count_ch2++;
	    }
	}

    printf("Test CH1 NUM : %d\n", count_ch1);
    printf("Test CH2 NUM : %d\n", count_ch2);
	printf("feof(fptr1) : %d !\n", feof(fptr1));
	printf("feof(fptr2) : %d !\n", feof(fptr2));
    printf("\n");
    printf("\n");
//	for(int i=0; i<996; i++)
//    {
//    	if(((i%30) == 0)&&(i>0))
//           printf("\n");
//    	printf("%02x ", test_read[i]);
//    }


    fclose(fptr1);
    fclose(fptr2);
    return 0;
}


/* dat文件写入*/
int dat_write(const char *filename1, unsigned char *data, int len)
{
    FILE* fptr1 = fopen(filename1 , "ab");


	if (fptr1 == NULL)
	{
	    puts("Fail to open read_file1 !");
	    return -1;
	}


	fwrite(data, sizeof(unsigned char), len, fptr1);



    fclose(fptr1);

    return 0;
}

/* RS422接收消息 */
void *thread_dma_422(int argc, char **argv)
{
	int rc, rxret_0 = 0, rxret_1 = 0;
	int i = 0, tmp_val = 0;
	int rec_len = 0;
	const array_t *tx_chans, *rx_chans;

	memset(&trans_gtx, 0, sizeof(trans_gtx));//GTX通道

	if (parse_args(argc, argv, &trans_gtx.input_channel, &trans_gtx.output_channel, &trans_gtx.output_size) < 0) {
		rc = 1;
		printf("parse_args_gtx \n");
	}


/* DMA通道初始化 */
	axidma_dev = axidma_init();
	if (axidma_dev == NULL)
	{
		fprintf(stderr, "Error: Failed to initialize the AXI DMA device.\n");
		rc = 1;
	}
	tx_chans = axidma_get_dma_tx(axidma_dev);
	if (tx_chans->len < 1) {
		fprintf(stderr, "Error: No transmit channels were found.\n");
		rc = -ENODEV;
	}
	rx_chans = axidma_get_dma_rx(axidma_dev);
	if (rx_chans->len < 1) {
		fprintf(stderr, "Error: No receive channels were found.\n");
		rc = -ENODEV;
	}

/* GTX发送、接收DMA通道  */
	trans_gtx.input_channel = tx_chans->data[0];
	trans_gtx.output_channel = rx_chans->data[0];
//	printf("\tGTX   Transmit Channel   : %d\n", trans_gtx.input_channel);
//	printf("\tGTX   Receive  Channel   : %d\n", trans_gtx.output_channel);

/* 初始化GTX通道 */
	trans_gtx.output_size = MAXLENGTH;
	trans_gtx.input_size = GTXMAXLENGTH;//GTX通道发送996字节数据

	trans_gtx.output_buf = axidma_malloc(axidma_dev, trans_gtx.output_size);
	if (trans_gtx.output_buf == NULL) {
		rc = -ENOMEM;
		axidma_free(axidma_dev, trans_gtx.output_buf, trans_gtx.output_size);
	}
	memset(trans_gtx.output_buf, 0, trans_gtx.output_size);

	trans_gtx.input_buf = axidma_malloc(axidma_dev, trans_gtx.input_size);
	if (trans_gtx.input_buf == NULL) {
		fprintf(stderr, "Failed to allocate the input buffer.\n");
		rc = -ENOMEM;
		axidma_free(axidma_dev, trans_gtx.input_buf, trans_gtx.input_size);
	}
	memset(trans_gtx.input_buf, 0, trans_gtx.input_size);

	/* *****测试写入***** */
//	const char *filename_a = "D:\\test\\a.txt";
//	const char *filename_b = "D:\\test\\b.txt";


	while (1)
	{

		/* 默认进入通道A，change_422等于1时，422切换至通道B */
		if( dma422_end == 0 )
		{
			rxret_0 = PCM_Recv(0, Recv_pcm0);//通道A接收数据
			rxret_1 = PCM_Recv(1, Recv_pcm1);//通道B接收数据
		}
	    else
	    	continue;

        /* RS422通道A --- PCM0接收数据*/
		if( (rxret_0 != 0) && (change_422 == 0) )
		{
			/* 测试 */
//			if(test_pcm < 1)
//			{
//			  test_pcm ++;
//			  printf("PCM---A---A RECEIVE DATA !\n");
//			}
//			else
//				test_pcm = 16;
//			printf("PCM---A---A RECEIVE DATA ! \n");
//			for (int j = 0; j < 1020; j++)
//			{
//				if( j%30 == 0 )
//					printf("\n");
//				printf("%02x ", Recv_pcm0[j]);
//			}
//			printf("\n");
//			dat_write(file_pcm1, Recv_pcm0, 1020);
//			acsdata_write(file_pcm1, Recv_pcm0, 1020);
//			acsdata_write(file_pcm1, &Recv_pcm0[8], 2);


			/* RS422通道接收正确帧个数*/
			if( count_pcm < 65535 )
				count_pcm ++;
			else
				count_pcm = 1;

			/* 数传B数据*/
			if( Recv_pcm0[25] == 0x33 )
			{
				Recv_pcm0[24]=0x5c;//GTX帧头
				Recv_pcm0[25]=0xfb;
				Recv_pcm0[1018]=0xfd;//GTX帧尾
				Recv_pcm0[1019]=0xfe;

				if( axidma0send(axidma_dev, &trans_gtx, &Recv_pcm0[24]) >= 0 )
				{
					/* 数传B通道数据GTX发送计数*/
					if( count_gtxsd < 16777215 )
						count_gtxsd ++;
					else
						count_gtxsd = 1;
				}
			}
			/* AI计算机数据 */
			else if( Recv_pcm0[25] == 0xcc )
			{
				dma422_end = 1;//RS422通道A接收完成标志变量
			}
		}
		/* RS422通道B --- PCM1接收数据*/
		else if( (rxret_1 != 0) && (change_422 == 1) )
		{
			/* RS422通道接收正确帧个数*/
			if( count_pcm < 65535 )
				count_pcm ++;
			else
				count_pcm = 1;

			/* 数传B数据*/
			if( Recv_pcm1[25] == 0x33 )
			{
				Recv_pcm1[24]=0x5c;//GTX帧头
				Recv_pcm1[25]=0xfb;
				Recv_pcm1[1018]=0xfd;//GTX帧尾
				Recv_pcm1[1019]=0xfe;
				if( axidma0send(axidma_dev, &trans_gtx, &Recv_pcm1[24]) >= 0 )
				{
					/* 数传B通道数据GTX发送计数*/
					if( count_gtxsd < 16777215 )
						count_gtxsd ++;
					else
						count_gtxsd = 1;
				}
			}
			/* AI计算机数据 */
			else if( Recv_pcm1[25] == 0xcc )
			{
				dma422_end = 2;//RS422通道B接收完成标志变量
			}
		}
	}
}



